package com.zywl.web.controller.sys;

import com.zywl.core.beans.User;
import com.zywl.service.LogService;
import com.zywl.service.UnitService;
import com.zywl.utils.models.PageData;
import com.zywl.utils.models.PageResult;
import com.zywl.utils.utils.Constants;
import com.zywl.utils.utils.MapUtil;
import com.zywl.utils.utils.enums.LogTypeEnum;
import com.zywl.web.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/log")
public class LogController extends BaseController {
    @Autowired
    private LogService logService;

    @GetMapping("/logListPage")
    public ModelAndView logListPage() {
        ModelAndView mav = new ModelAndView("log/logListPage");
        mav.addObject("logTypes", LogTypeEnum.values());
        return mav;
    }

    @PostMapping("/getLogList")
    public PageResult getLogList(HttpServletRequest request, PageData pageData) {
        Map<String, Object> condition = MapUtil.changeMap(request.getParameterMap());
        User user = (User) request.getSession().getAttribute(Constants.USER_SESSION_KEY);
        PageResult pageResult = null;
        try {
            if(user.getUsrAssociationId() != null){
                condition.put("logAssociationId",user.getUsrAssociationId());
            }
            pageResult = putToResult(logService.getPageInfo(condition, pageData));
        } catch (Exception e) {
            logger.error("获取日志列表异常", e);
        }
        return pageResult;
    }
}
